<?php

namespace Unicom\Controller;

use Think\Controller;

class SmallController extends AdminController
{

    public function list_()
    {
        layout(false);
        $model = M('wa.notify', 'small_');
        $REQUEST = (array)I('request.');
        $product_type = I('param.product_type', 0);
        $start=I('param.start',date('Y-m-d'));
        $end=I('param.end',date('Y-m-d'));
        $search = $REQUEST["search"];
        import('ORG.Util.Page'); // 导入分页类
        if (!empty($search)) {
            $where = "where oid like '$search%' or cal like '$search%' or channel=$search";
        }

        $sql = " select *,
  (select count(*) from wa.small_pay where  FROM_UNIXTIME(create_at, '%Y-%m-%d') BETWEEN  '$start' and '$end' and channel=a.channel and fee=a.fee) as code_count,
        (select count(*) from wa.small_pay where LOCATE('成功',resp)  and  FROM_UNIXTIME(create_at, '%Y-%m-%d') BETWEEN  '$start' and '$end' and channel=a.channel and fee=a.fee) as code_success_count,
        (select count(*) from wa.small_notify where `code`='0000'  and  FROM_UNIXTIME(create_at, '%Y-%m-%d') BETWEEN  '$start' and '$end' and channel=a.channel and amount=a.fee) as success_count,
        (select count(*) from wa.small_notify where `code`='0000'  and  FROM_UNIXTIME(create_at, '%Y-%m-%d') BETWEEN  '$start' and '$end' and channel=a.channel and amount=a.fee and sync=1) as sync_count
        
 from 
 (
 select FROM_UNIXTIME(a.create_at, '%Y-%m-%d') as today,a.channel,a.fee,count(*) as up_count
        
        
        from (select * from wa.small_order where FROM_UNIXTIME(create_at, '%Y-%m-%d') BETWEEN  '$start' and '$end' )  as a
	
        group by channel,a.fee
        order by a.create_at desc) as a";
        $data = $model->query($sql);

        // var_dump($data);

        $this->assign('data', $data);
        $this->assign("start",$start);
        $this->assign("end",$end);

        $this->display();

    }

    //查找

    public function find_()
    {
        layout(false);
        $model = M('wa.small_order', '');

        $phone = I('param.phone');

        $sql = "
        select b.payer as cal,b.channel,FROM_UNIXTIME(a.create_at, '%Y-%m-%d %h:%i:%s') as create_at,p.dcprivence,a.msg
        from wa.small_notify as a
	left join wa.small_order as b on a.order_seq=b.order_seq
        left join cp.cp_privence as p on left (b.payer,7)=p.dcmobile
        where b.payer='$phone' 
        
        order by a.create_at desc
        ";
        $data = $model->query($sql);


        $this->assign('data', $data);
        $this->display();

    }


    //上行详情

    public function upinfo_()
    {
        layout(false);
        $model = M('wa.small_order', '');
        $start = I('param.start');
        $end = I('param.end');
        $channel = I('param.channel');

        $sql = "
        select a.payer,a.channel,FROM_UNIXTIME(create_at, '%Y-%m-%d %H:%i:%s') as create_at,p.dcprivence, a.resp
        from wa.small_order as a
        left join cp.cp_privence as p on left (a.payer,7)=p.dcmobile
        where FROM_UNIXTIME(a.create_at, '%Y-%m-%d') BETWEEN  '$start' and '$end' and a.channel='$channel' 
        
        order by a.create_at desc
        ";
        $data = $model->query($sql);


        $this->assign('data', $data);
        $this->display("info_");

    }

    //验证码详情

    public function codeinfo_()
    {
        layout(false);
        $model = M('wa.small_order', '');
        $start = I('param.start');
        $end = I('param.end');
        $channel = I('param.channel');
        $fee = I('param.fee');

        $sql = "
        select b.payer,b.channel,FROM_UNIXTIME(a.create_at, '%Y-%m-%d %H:%i:%s') as create_at,p.dcprivence,a.resp
        from wa.small_pay as a
        inner join wa.small_order as b on a.trans_seq=b.transSeq
        left join cp.cp_privence as p on left (b.payer,7)=p.dcmobile
        where FROM_UNIXTIME(a.create_at, '%Y-%m-%d') BETWEEN  '$start' and '$end' and b.channel='$channel' and a.fee='$fee'
        
        order by a.create_at desc
        ";
        $data = $model->query($sql);


        $this->assign('data', $data);
        $this->display("info_");

    }

    //成功详情

    public function successinfo_()
    {
        layout(false);
        $model = M('wa.small_order', '');
        $start = I('param.start');
        $end = I('param.end');
        $channel = I('param.channel');

        $sql = "
        select b.payer,b.channel,FROM_UNIXTIME(a.create_at, '%Y-%m-%d %H:%i:%s') as create_at,p.dcprivence
        from wa.small_notify as a
        inner join wa.small_order as b on a.order_seq=b.order_seq
        left join cp.cp_privence as p on left (b.payer,7)=p.dcmobile
        where FROM_UNIXTIME(a.create_at, '%Y-%m-%d') BETWEEN  '$start' and '$end' and b.channel='$channel' and a.code='0000' 
        
        order by a.create_at desc
        ";
        $data = $model->query($sql);


        $this->assign('data', $data);
        $this->display("info_");

    }

    //成功详情

    public function syncinfo_()
    {
        layout(false);
        $model = M('wa.small_order', '');
        $start = I('param.start');
        $end = I('param.end');
        $channel = I('param.channel');

        $sql = "
        select b.payer,b.channel,FROM_UNIXTIME(a.create_at, '%Y-%m-%d %H:%i:%s') as create_at,p.dcprivence
        from wa.small_notify as a
        inner join wa.small_order as b on a.order_seq=b.order_seq
        left join cp.cp_privence as p on left (b.payer,7)=p.dcmobile
        where FROM_UNIXTIME(a.create_at, '%Y-%m-%d') BETWEEN  '$start' and '$end' and b.channel='$channel' and a.code='0000' and a.sync=1
        
        order by a.create_at desc
        ";
        $data = $model->query($sql);


        $this->assign('data', $data);
        $this->display("info_");

    }


    public function index()
    {
        layout(false);
        $this->assign('date', date('Y-m-d'));
        $this->display("privence");
    }

    public function statByPrivence_()
    {
        layout(false);
        $pdate_start = I('param.pdate_start', date("Y-m-d"));
        $pdate_end = I('param.pdate_end', date("Y-m-d"));

        $pchannel = I('param.pchannel', 0);

        $this->assign("pdate_start", $pdate_start);
        $this->assign("pdate_end", $pdate_end);
        $this->assign("pchannel", $pchannel);
        $model = M();
        $channels = $model->query("select id as `key` from wa.wa_auth_key where product=8");

        $this->assign("channels", $channels);

        if (IS_GET) {

            $this->display("privence");
            return;
        }
        $where_channel = "";
        if ($pchannel > 0){
            $where_channel = "and a.channel=$pchannel ";
        }

        $sql = "
         select FROM_UNIXTIME(a.create_at, '%Y-%m-%d %H:%i:%s') as create_at,p.dcprivence,count(*) as count
        from wa.small_notify as a 
	left join wa.small_order as b on a.order_seq=b.order_seq
        left join cp.cp_privence as p on left (b.payer,7)=p.dcmobile
        where a.code='0000'  $where_channel and FROM_UNIXTIME(a.create_at, '%Y-%m-%d') BETWEEN '$pdate_start' and '$pdate_end' 
        GROUP BY p.dcprivence;";

        $data = $model->query($sql);

        $result["data"] = $data;
        $this->assign("data",$data);
        $this->display("privence");

    }

    public function infoByChannel()
    {
        layout(false);
        $pdate = I('param.pdate', date("Y-m-d"));
        $pchannel = I('param.pchannel', 1011);
        $model = M();

        $sql1 = "select FROM_UNIXTIME(create_at, '%Y-%m-%d %H:%i:%s') as create_at,p.dcprivence,count(*) as count 
        from wa.ivr_pricing_param as a 
         left join cp.cp_privence as p on left (a.cal,7)=p.dcmobile
        where  FROM_UNIXTIME(create_at, '%Y-%m-%d')='$pdate' and a.channel=$pchannel
        GROUP BY p.dcprivence;";
        $sql3 = "select FROM_UNIXTIME(create_at, '%Y-%m-%d %H:%i:%s') as create_at,p.dcprivence,count(*) as count 
        from wa.ivr_pricecb_result as a 
         left join cp.cp_privence as p on left (a.cal,7)=p.dcmobile
        where a.cod=200  and a.sync=1 and FROM_UNIXTIME(create_at, '%Y-%m-%d')='$pdate' and a.channel=$pchannel
        GROUP BY p.dcprivence;";
        $sql2 = "select FROM_UNIXTIME(create_at, '%Y-%m-%d %H:%i:%s') as create_at,p.dcprivence,count(*) as count 
        from wa.ivr_code_param as a 
         left join cp.cp_privence as p on left (a.cal,7)=p.dcmobile
        where  a.code=200 and FROM_UNIXTIME(create_at, '%Y-%m-%d')='$pdate' and a.channel=$pchannel
        GROUP BY p.dcprivence;";

        $sql4 = "select FROM_UNIXTIME(create_at, '%Y-%m-%d %H:%i:%s') as create_at,p.dcprivence,count(*) as count 
        from wa.ivr_code_param as a 
         left join cp.cp_privence as p on left (a.cal,7)=p.dcmobile
        where   FROM_UNIXTIME(create_at, '%Y-%m-%d')='$pdate' and a.channel=$pchannel
        GROUP BY p.dcprivence;";
        $data1 = $model->query($sql1);

        $data2 = $model->query($sql2);

        $data3 = $model->query($sql3);
        $data4 = $model->query($sql4);
        $ret['pricing'] = $data1;
        $ret['code'] = $data2;
        $ret['code2'] = $data4;
        $ret['sync'] = $data3;
        $this->assign("pdate", $pdate);
        $this->assign("pchannel", $pchannel);
        $this->assign("data", $ret);
        $this->display("channel_info");


    }


    public function stat(){
        layout(false);
        $model=M();
        $sql="select count(*) as total_count,FROM_UNIXTIME(create_at,'%Y-%m-%d') as today,
        (select count(*) from wa.small_notify where FROM_UNIXTIME(create_at,'%Y-%m-%d')=today and `code`='0000') as success_count
        from wa.small_notify as a
        group by today
        order by a.create_at desc";
        $data= $model->query($sql);
        $this->assign('data',$data);
        $this->display();
    }

}